package services;

import br.com.caelum.vraptor.ioc.Component;
import entities.UsersEntity;
import logs.LogManager;
import managers.CreateUserException;
import managers.UserManager;

@Component
public class UserService {
    private final UserManager userManager;

    public UserService(UserManager userManager) {
        this.userManager = userManager;
    }

    public void createNewUser(String username, String email, String password) throws CreateUserException {
        if(username == null || username.trim() == ""){
            throw new CreateUserException("UserName Is Empty");
        }

        if(email == null || email.trim() == ""){
            throw new CreateUserException("Email Is Empty");
        }

        if(password == null || password.trim() == ""){
            throw new CreateUserException("Password Is Empty");
        }

        if(userManager.checkUserExists(username)){
            throw new CreateUserException("User " + username + " exists");
        }

        UsersEntity user = userManager.createNewUser(username, email, password);

        LogManager.logForUser(user.getUserId()).write("User Created: ", username);
        LogManager.logForProduct(0).write("User Created: ", username);
    }
}
